package cn.lbaicai.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.lbaicai.common.CurrentUser;
import cn.lbaicai.core.BaseController;
import cn.lbaicai.entity.RequestData;
import cn.lbaicai.entity.ResultData;
import cn.lbaicai.entity.Role;
import cn.lbaicai.entity.User;
import cn.lbaicai.service.RoleService;
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController{
	@Autowired
	private RoleService roleService;
	//private 
	@RequestMapping("/list")
	public String getRole(){
		return "role/list";
	}
	@RequestMapping("/roleData")
	@ResponseBody
	public String roleData(int page,int limit){
		RequestData<Role> requestData = new RequestData<Role>();
		requestData.setPage(page);
		requestData.setLimit(limit);
		requestData.setpLimit((page-1)*limit);
		ResultData<List<Role>> resultData=roleService.selectRole(requestData);
		return JSON.toJSONString(resultData);
	}
	@RequestMapping(value="/addRole",method=RequestMethod.GET)
	public String addRole(){
		return "role/addRole";
	}
	@RequestMapping(value="/addRole",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> addRole(@CurrentUser User user,Role role){
		Map<String,Object> resultMap = new HashMap<String, Object>();
		role.setUid(user.getId());
		role.setCreater(user.getUsername());
		int addStatus = roleService.insertSelective(role);
		if(addStatus>0){
			resultMap.put("code", 200);
			resultMap.put("msg","添加成功");
		}else{
			resultMap.put("code",201);
			resultMap.put("msg","添加失败");
		}
		return resultMap;
	}
	@RequestMapping(value="/verify",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> verify(Role role){
		Map<String,Object> resultData = new HashMap<String, Object>();
		List<Role> lRole = roleService.selectByRole(role);
		if(lRole.size()>0){
			resultData.put("code", 200);
			resultData.put("msg", "已存在");
		}else{
			resultData.put("code", 201);
			resultData.put("msg", "未存在");
		}
		return resultData;
	}
	@RequestMapping(value="/deleteRole")
	@ResponseBody
	public Map<String,Object> deleteRole(int id){
		Map<String,Object> resultData = new HashMap<String, Object>();
		resultData.put("code",201);
		if(id>0){
			int status = roleService.deleteByPrimaryKey(id);
			if(status>0){
				resultData.put("code", 200);
				resultData.put("msg", "删除成功");
			}else{
				resultData.put("msg", "删除失败");
			}
		}else{
			resultData.put("msg", "非法操作");
		}
		
		return resultData;
	}
	@RequestMapping("/delAllRole")
	@ResponseBody
	public Map<String,Object> delAllRole(String ids){
		Map<String,Object> resultData = new HashMap<String, Object>();
		resultData.put("code",201);
		if(StringUtils.isNotBlank(ids)){
			int status = roleService.delAllRole(ids);
			if(status>0){
				resultData.put("code", 200);
				resultData.put("msg", "删除成功");
			}else{
				resultData.put("msg", "删除失败");
			}
		}else{
			resultData.put("msg", "非法操作");
		}
		return resultData;
	}
}
